#define _CRT_SECURE_NO_WARNINGS 1
#include"zhan.h"

void StackInit(Zhan* ps)
{
	assert(ps);
	ps->_a = NULL;
	ps->_capacity = 0;
	ps->_top = 0;
}

void StackPush(Zhan* ps, STDataType data)
{
	assert(ps);
	if (ps->_top == ps->_capacity)
	{
		int new = ps->_capacity == 0 ? 4 : ps->_capacity * 2;
		STDataType* temp = (STDataType*)realloc(ps->_a, sizeof(STDataType) * new);
		if (temp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		ps->_a = temp;
		ps->_capacity = new;

	}

	ps->_a[ps->_top] = data;
	ps->_top++;
}

void StackPop(Zhan* ps)
{
	assert(ps);
	assert(ps->_top>0);
	ps->_top--;
}

STDataType StackTop(Zhan* ps)
{
	assert(ps);
	assert(ps->_top > 0);
	return ps->_a[ps->_top - 1];
}

int StackSize(Zhan* ps)
{
	assert(ps);
	return ps->_top;
}

int StackEmpty(Zhan* ps)
{
	assert(ps);
	return ps->_top == 0;
}

void StackDestroy(Zhan* ps)
{
	assert(ps);
	free(ps->_a);
	ps->_a = NULL;
	ps->_capacity = ps->_top = 0;
}

void Print(Zhan* ps)
{
	assert(ps);
	while (!StackEmpty(ps))
	{
		printf("%d ", StackTop(ps));
		StackPop(ps);
	}
	printf("\n");
}